//
//  ViewController.m
//  08-UITableViewHomeWork
//
//  Created by 曹曹 on 16/7/25.
//  Copyright © 2016年 曹曹. All rights reserved.
//

#import "ViewController.h"
#import "DetailViewController.h"
#import "AddStudentViewController.h"

#define kScreenWidth self.view.frame.size.width
#define kScreenHeight self.view.frame.size.height
#define kreuseIdentifier @"reuseIdentifier"

@interface ViewController () <UITableViewDataSource, UITableViewDelegate, AddStudentProtocol>
@property (nonatomic, strong) NSMutableArray *studentArray;
@property (nonatomic, strong) UITableView *tableView;
@end

@implementation ViewController
/**
 * 
 1.根据给定的plist文件(讲师教给学员怎么使用，读取数据）, 使用tableView写一个通讯录, 要求:
 第一个页面每个cell上显示学员姓名
 点击某个学员, 推出到详细信息页面
 详细信息页面使用label显示学员的详细信息
 NSString *path = [[NSBundle mainBundle] pathForResource:@"DLS160710" ofType:@"plist"];
 NSArray *array = [NSArray arrayWithContentsOfFile:path];
 2.
 在导航栏上增加一个按钮可以push一个添加学员页面
 添加学员页面中使用textField让用户输入信息, 点击保存按钮将学员信息显示到第一页tableView的最后面
 提示:
 tableView有一个方法- (void)reloadData; 可以重载所有的dataSource协议方法. 因此可以把数据添加到数组中, 然后reloadData即可.
 使用协议传值 
 */
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.shadowImage = [UIImage new];
    self.navigationController.navigationBar.translucent = YES;
    UIView *backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 64)];
    backgroundView.backgroundColor = [UIColor whiteColor];
    NSString *path = [[NSBundle mainBundle] pathForResource:@"DLS160710-2" ofType:@"plist"];
    NSArray *array = [NSArray arrayWithContentsOfFile:path];
    NSLog(@"---------------%@", array);
    self.studentArray = [NSMutableArray arrayWithArray:array];
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:kreuseIdentifier];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addStudent:)];
    [self.view addSubview:_tableView];
    [self.view addSubview:backgroundView];
    self.navigationItem.leftBarButtonItem = self.editButtonItem;
}
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
    [super setEditing:editing animated:animated];
    [self.tableView setEditing:editing animated:animated];
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [_studentArray removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
    }
}
// 添加学生按钮点击事件
- (void)addStudent:(UIBarButtonItem *)barButton {
    AddStudentViewController *addStudentVC = [[AddStudentViewController alloc] init];
    addStudentVC.delegate = self;
    [self.navigationController pushViewController:addStudentVC animated:YES];
}
#pragma mark - 协议方法
- (void)addStudentWithDictionary:(NSDictionary *)diction {
    if (![[diction objectForKey:@"name"] isEqualToString:@""]) {
        [_studentArray addObject:diction];
    }
    [self.tableView reloadData];
}
#pragma mark - tableView Datasource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _studentArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kreuseIdentifier];
    NSDictionary *studentDiction = [_studentArray objectAtIndex:indexPath.row];
    NSString *name = [studentDiction objectForKey:@"name"];
    cell.textLabel.text = name;
    return cell;
}
#pragma mark - tableView Deledate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary *studentDiction = [_studentArray objectAtIndex:indexPath.row];
    DetailViewController *detailVC = [[DetailViewController alloc] init];
    detailVC.name = [studentDiction objectForKey:@"name"];
    NSNumber *number = [studentDiction objectForKey:@"age"];
    NSString *age = [[[NSNumberFormatter alloc] init] stringFromNumber:number];
    detailVC.age = age;
    detailVC.gender = [studentDiction objectForKey:@"gender"];
    detailVC.phoneNumber = [studentDiction objectForKey:@"phoneNumber"];
    [self.navigationController pushViewController:detailVC animated:YES];
}
#pragma mark - Other
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
